export function ifNaNOrNull(n: number | undefined, def: number): number {
  if (isNaNOrNull(n)) {
    return def
  }
  return n!
}

export function isNaNOrNull(n: number | undefined): boolean {
  if (n == null) {
    return true
  }
  return isNaN(n)
}

export function formatInt(i: number): string {
  if (i < 10) {
    return '0' + i
  }
  return i + ''
}

export function compare(l0: number[] | undefined, l1: number[] | undefined): boolean {
  if (!l0 && !l1) {
    return true
  }
  if (!l0 || !l1) {
    return false
  }
  if (l0.length !== l1.length) {
    return false
  }
  for (const i in l0) {
    if (!numEqual(l0[i], l1[i])) {
      return false
    }
  }
  return true
}

export function numEqual(v0: number | undefined, v1: number | undefined): boolean {
  if (v0 == null && v1 == null) {
    return true
  }
  if (v1 == null || v0 == null) {
    return false
  }
  if (isNaN(v0!) && isNaN(v1!)) {
    return true
  }
  return v0 === v1
}
